//# sourceURL=http://uMapApp/UI/Panel/PanelBlock/TableBlock
define([
    "mhjs/mhjs"
    , "./PanelBlock"
    , "../Item/ListItem"
    , "./Filter"
    , "./Sorter"
], function (
    mhjs,
    PanelBlock,
    ListItem,
    Filter,
    Sorter
) {

    function TableBlock(option) {

        var _this = this;
        _this.isOpened = false;

        _this._head = [];
        _this._body = [];
        _this._foot = [];

        PanelBlock.call(_this, option);
        _this.addClass("tableBlock");

        _this.ui = _this.ui || {};

        _this.ui.filterBlock = new mhjs.Div({"class": "filterBlock"});

        _this.ui.table = new mhjs.Table();

        _this.ui.head = new mhjs.Thead({"class": "head"});
        _this.ui.head.tr = new mhjs.Tr();
        _this.ui.head
            .append(_this.ui.head.tr);
        _this.ui.body = new mhjs.Tbody({"class": "body"});
        _this.ui.foot = new mhjs.Tfoot({"class": "foot"});
        _this.ui.foot.tr = new mhjs.Tr();
        _this.ui.foot
            .append(_this.ui.foot.tr);

        _this.ui.table
            .append(_this.ui.head)
            .append(_this.ui.body)
            .append(_this.ui.foot)
        ;

        _this.ui.wrap
            .append(_this.ui.filterBlock)
            .append(_this.ui.table)
        ;

    }

    var _class = TableBlock;
    mhjs.Inherit(_class, PanelBlock);


    Object.defineProperties(_class.prototype, {
        "head"     : {
            set: function (list) {
                var _this = this;
                _this._head = [];
                _this.ui.head.tr.empty();

                $.each(list, function (i, item) {

                    var th = new mhjs.Th();
                    th.text(item.label);

                    item.th = th;

                    _this.ui.head.tr.append(th);

                    if (item.col) {
                        th.css(item.col);
                    }

                    _this._head.push(item);
                });

            },
            get: function () {
                return this._head;
            }
        },
        "body"     : {
            set: function (list) {
                var _this = this;
                $.each(list, function (i, item) {
                    _this.add(item);
                });
            },
            get: function () {
                return this._body;
            }
        },
        "add"      : {
            value: function (item, formatFunction) {
                var _this = this;

                var tr = new mhjs.Tr();

                $.each(_this.head, function (i, header) {

                    var td = new mhjs.Td();

                    tr.append(td);

                    if (header.col) {
                        td.css(header.col);
                    }

                    if (header.value) {
                        if (item.getChainChild(header.value) !== undefined) {

                            if (header.format) {
                                header.format(item, td);
                            } else {
                                td.text(item.getChainChild(header.value));
                            }

                        } else {
                            td.addClass("noData");
                        }
                    } else {
                        if (header.format) {
                            header.format(_this, td);
                        }
                    }

                    if (header.autoCount) {
                        td.text(header.autoCount(_this.body.length + 1));
                    }


                    if (header.tag) {
                        tr[header.tag] = td;
                    }

                });

                _this._body.push(item);
                _this.ui.body.append(tr);

                if (formatFunction) {
                    formatFunction(item, tr);
                }

                return tr;

            }
        },
        "foot"     : {
            set: function (list) {
                var _this = this;
                _this._foot = list;
            },
            get: function () {
                return this._foot;
            }
        },
        "clear"    : {
            value: function () {
                var _this = this;
                _this._body = [];
                _this.ui.body.empty();
            }
        },
        "filter"   : {
            set: function (filter) {
                var _this = this;
                filter._list = _this.body;
                _this._filter = new Filter(filter);
                _this.ui.filterBlock.append(_this.filter);

                _this.filter.on("update", function () {
                    $.each(_this.filter.filter.failIndexs, function (i, item) {
                        _this.body[item].listItem.hide();
                    });
                    $.each(_this.filter.filter.okIndexs, function (i, item) {
                        _this.body[item].listItem.show();
                    });
                });

            },
            get: function () {
                return this._filter;
            }
        },
        "sorter"   : {
            set: function (sorter) {
                var _this = this;
                sorter._list = _this.body;
                _this._sorter = new Sorter(sorter);
                _this.ui.filterBlock.append(_this.sorter);

                _this.sorter.on("update", function () {
                    $.each(_this.sorter.sorter.resultIndexs, function (i, index) {
                        _this.body[index].listItem.dom.detach().appendTo(_this.ui.body.dom);
                    });
                });

            },
            get: function () {
                return this._sorter;
            }
        },
        "exportXls": {
            value: function (title) {
                var _this = this;
                mhjs.Tool.table2xls(_this.ui.table.dom[0], title);
            }
        }
    });

    return _class;

});